package com.example.libraryapp.lend;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.example.libraryapp.R;
import com.example.libraryapp.book.BookInfo;
import com.example.libraryapp.book.BookService;
import com.example.libraryapp.db.MySqliteHelper;
import com.example.libraryapp.user.UserInfo;
import com.example.libraryapp.util.Constance;
import com.example.libraryapp.util.MyConfig;

/**
 * 借阅服务，主要提供对借阅信息的管理
 */
public class LendService {
	/**
	 * 得到所有的借阅信息
	 * 
	 * @return
	 */
	public static List<LendInfo> getAllLend(Context context) {

		List<LendInfo> LendList = null;
		String cardid = MyConfig.getUserInfo(context).getCardid();
		String where = "cardid = " + cardid;
		Cursor cursor = MySqliteHelper.getInstance(context).query(
				Constance.LEND_TABLE_NMAE, where);
		if (cursor != null) {
			LendList = new ArrayList<LendInfo>();
			while (cursor.moveToNext()) {
				LendInfo LendInfo = new LendInfo();
				LendInfo.setId(cursor.getInt(cursor.getColumnIndex("id")));
				String bookid = cursor.getString(cursor
						.getColumnIndex("bookid"));
				LendInfo.setBookInfo(BookService.find(context, bookid));
				LendInfo.setCardid(cursor.getString(cursor
						.getColumnIndex("cardid")));
				LendInfo.setUsername(cursor.getString(cursor
						.getColumnIndex("username")));
				LendInfo.setStarting(cursor.getString(cursor
						.getColumnIndex("starting")));
				LendInfo.setExpiring(cursor.getString(cursor
						.getColumnIndex("expiring")));
				LendList.add(LendInfo);
			}
		}
		return LendList;
	}

	/**
	 * 根据条件得到借阅
	 * 
	 * @return
	 */
	public static List<LendInfo> getLendInfoByParam(Context context,
			String param) {
		List<LendInfo> LendList = null;
		String where = "username like '%" + param + "%'";
		where += " or ";
		where += "cardid like '%" + param + "%'";
		Cursor cursor = MySqliteHelper.getInstance(context).query(
				Constance.LEND_TABLE_NMAE, where);
		if (cursor != null) {
			LendList = new ArrayList<LendInfo>();
			while (cursor.moveToNext()) {
				LendInfo LendInfo = new LendInfo();
				LendInfo.setId(cursor.getInt(cursor.getColumnIndex("id")));
				String bookid = cursor.getString(cursor
						.getColumnIndex("bookid"));
				LendInfo.setBookInfo(BookService.find(context, bookid));
				LendInfo.setCardid(cursor.getString(cursor
						.getColumnIndex("cardid")));
				LendInfo.setUsername(cursor.getString(cursor
						.getColumnIndex("username")));
				LendInfo.setStarting(cursor.getString(cursor
						.getColumnIndex("starting")));
				LendInfo.setExpiring(cursor.getString(cursor
						.getColumnIndex("expiring")));
				LendList.add(LendInfo);
			}
		}
		return LendList;
	}

	/**
	 * 是否已经借过了
	 * 
	 * @return
	 */
	public static boolean alreadyLend(Context context, String bookid,
			String cardid) {
		String where = "cardid='" + cardid + "'";
		where += " and ";
		where += "bookid='" + bookid + "'";
		Cursor cursor = MySqliteHelper.getInstance(context).query(
				Constance.LEND_TABLE_NMAE, where);
		if (cursor != null) {
			if (cursor.moveToNext()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 借书
	 * 
	 * @param context
	 * @param bookInfo
	 * @return
	 */
	public static boolean lend(Context context, BookInfo bookInfo) {
		ContentValues values = new ContentValues();
		// id, bookid, cardid, username, starting, expiring)
		// VALUES ('1','1','111003301012','陈松蔚','2015-12-01','2016-01-02')"
		UserInfo userInfo = MyConfig.getUserInfo(context);
		values.put("bookid", bookInfo.getId());
		values.put("cardid", userInfo.getCardid());
		values.put("username", userInfo.getUsername());
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);

		values.put("starting", year + "-" + (month + 1) + "-" + day);
		values.put("expiring", year + "-" + (month + 2) + "-" + day);
		long lendResult = MySqliteHelper.getInstance(context).insert(
				Constance.LEND_TABLE_NMAE, null, values);
		if (lendResult != -1) {
			return true;
		}
		return false;
	}

	/**
	 * 借书
	 * 
	 * @param context
	 * @param bookInfo
	 * @return
	 */
	public static boolean relend(Context context, LendInfo lendInfo) {
		ContentValues values = new ContentValues();
		// id, bookid, cardid, username, starting, expiring)
		// VALUES ('1','1','111003301012','陈松蔚','2015-12-01','2016-01-02')"
		UserInfo userInfo = MyConfig.getUserInfo(context);
		String exporingTime = lendInfo.getExpiring();
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(exporingTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);

		values.put("expiring", year + "-" + (month + 2) + "-" + day);
		String where = " id=" + lendInfo.getId();
		int relendResult = MySqliteHelper.getInstance(context).update(
				Constance.LEND_TABLE_NMAE, values, where, null);
		if (relendResult > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 删除表
	 * 
	 * @param context
	 */
	public static void clear(Context context) {
		MySqliteHelper.getInstance(context).clearCache(
				Constance.LEND_TABLE_NMAE);
	}
}
